import os
import sys
import re
from matplotlib import pyplot as plt



class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'


fps = re.compile('Protocol:(.*):(.*)')


fn = sys.argv[1]

lines = open(fn).readlines()
fpss = []

num = 1
total = 1
xv = {}
nv = {}
protoList = {}

for l in lines:
    ret = fps.findall(l)
    if len(ret) > 0:
       
        #print ret[0][0]
        pid = ret[0][0]
        size = ret[0][1]
        if pid not in xv:
            xv[pid] = 0
            nv[pid] = 0
            protoList[pid] = {}

        isz = int(size) 
        xv[pid] += isz
        nv[pid] += 1

        if isz not in protoList[pid]:
            protoList[pid][isz] = 0
        protoList[pid][isz] += 1

#xv = nv


rows = []    
for p in xv:
    rows.append([p, xv[p]])


averageSize = {}
for p in xv:
    averageSize[p] = [xv[p]*1.0/nv[p], nv[p]]

rows2 = []
for p in averageSize:
    rows2.append([p] + averageSize[p])

def cmp1(a, b):
    if a[0] > b[0]:
        return -1
    elif a[0] < b[0]:
        return 1
    else:
        return 0

print "ProtoSizeList"
for p in protoList:
    print p
    rows3 = []
    for sz in protoList[p]:
        rows3.append([sz, protoList[p][sz]])

    rows3.sort(cmp=cmp1)
    for r in rows3:
        print r



#protoId, 
#size1 num
#size2 num

#protoId



def cmp(a, b):
    if a[1] > b[1]:
        return -1
    elif a[1] < b[1]:
        return 1
    else:
        return 0
rows.sort(cmp=cmp)

rows2.sort(cmp=cmp)

print "PacketSize"
for r in rows2:
    print r


print "Count"

l = len(rows)
width = 1.0/l

n = 0
sizes = []

ind = []
for r in rows:
    print r
    sizes.append(r[1])
    ind.append(n)
    n += 1

plt.bar(ind, sizes)  



plt.show()
